﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.BusinessEntities;
namespace Skynet.BusinessLogic.DataAccess
{
    public class ExamRepository : MasterRepository<Exam>, IExamRepository
    {
        public ExamRepository(IUnityContainer container)
            : base(container)
        { 


        }
        public override Exam GetOne(object key)
        {
            var id = (Guid)key;
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Exams.Include("ExamCategory") where c.Id == id select c).SingleOrDefault();
            return query;
        }
        public IEnumerable<Exam> GetByCustomerId(Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Exams.Include("ExamCategory") where c.CreatedBy == CustomerId select c);
            return query.ToList();            
        }
        public IEnumerable<Exam> GetByCustomerIdCategoryId(Guid CategoryId, Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Exams.Include("ExamCategory") where c.CreatedBy == CustomerId && c.CategoryExamId == CategoryId select c);
            return query.ToList();            
        }
        public IEnumerable<Exam> GetDesignByCustomerIdCategoryId(Guid CategoryId, Guid CustomerId)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Exams.Include("ExamCategory") where c.CreatedBy == CustomerId && c.CategoryExamId == CategoryId select c);
            return query.ToList();            
        }
        public override void Add(Exam item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            ctx.Exams.AddObject(item);
            ctx.SaveChanges();
            item.AcceptChanges();
            
        }
        public override void Update(Exam item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.Exams where c.Id == item.Id select c).FirstOrDefault();
            if (original == null) throw new System.Data.DeletedRowInaccessibleException("This record have been deleted by another user.");            
            ctx.Exams.ApplyCurrentValues(item);
            ctx.SaveChanges();
            item.AcceptChanges();
            
        }
        public override void Delete(Exam item)
        {            
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Exams where c.Id == item.Id select c).SingleOrDefault();
            if (query != null)
            {
                ctx.Exams.DeleteObject(query);
                ctx.SaveChanges();
                item.AcceptChanges();
            }
        }
        public override IEnumerable<Exam> GetAll()
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Exams.Include("ExamCategory") select c);
            return query.ToList();
        }
        
    }
}
